package com.xxx.controller;

import com.xxx.common.Result;
import com.xxx.common.ResultUtil;
import com.xxx.eneity.Contest;
import com.xxx.eneity.ContestNotice;
import com.xxx.eneity.ContestSpecies;
import com.xxx.service.ContestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1.0")
public class ContestController {

    @Autowired
    private ContestService contestService;

    @GetMapping("/getContestSpeciesList")
    public Result getContestSpeciesList() {
        List<ContestSpecies> contestSpeciesList = contestService.getContestSpeciesList();
        return ResultUtil.success(contestSpeciesList);
    }

    @PostMapping("/addContestSpecies")
    public Result addContest(@RequestParam String contestSpeciesName){
        int i = contestService.addContestSpecies(contestSpeciesName);
        if (i > 0) {
            return ResultUtil.success("添加赛事板块成功");
        } else {
            return ResultUtil.error(1001, "添加赛事板块失败");
        }
    }

    @PostMapping("/editContestSpecies")
    public Result editContestSpecies(@RequestParam Integer contestSpeciesId, @RequestParam String contestSpeciesName) {
        int i = contestService.editContestSpecies(contestSpeciesId, contestSpeciesName);
        if (i > 0) {
            return ResultUtil.success("修改赛事板块名称成功");
        } else {
            return ResultUtil.error(2001, "修改赛事板块失败");
        }
    }

    @DeleteMapping("/deleteContestSpecies")
    public Result deleteContestSpecies(@RequestParam Integer contestSpeciesId) {
        int i = contestService.removeContestSpecies(contestSpeciesId);
        if (i > 0) {
            return ResultUtil.success("删除赛事板块成功");
        } else {
            return ResultUtil.error(1002, "删除赛事板块失败");
        }
    }

    @GetMapping("/getContestList")
    public Result getContestList(Integer contestSpeciesId,  Integer status) {
        List<Contest> contestList = contestService.getContestList(contestSpeciesId, status);
        return ResultUtil.success(contestList);
    }

    @PostMapping("/addContest")
    public Result addContest(@RequestBody Contest contest) {
        if (contest.getSpeciesId() == null) {
            return ResultUtil.error(1005, "赛事板块编号不能为空");
        } else if (contest.getName() == null) {
            return ResultUtil.error(1006, "赛事名称不能为空");
        } else if (contest.getStartDate() == null) {
            return ResultUtil.error(1015, "赛事开始时间不能为空");
        } else if (contest.getEndDate() == null) {
            return ResultUtil.error(1016, "赛事截止时间不能为空");
        }
        int result = contestService.addContest(contest);
        if (result > 0) {
            return ResultUtil.success("添加赛事成功");
        } else {
            return ResultUtil.error(1007, "添加赛事失败");
        }
    }

    @PostMapping("/editContest")
    public Result editContest(@RequestBody Contest contest) {
        if (contest.getId() == null) {
            return ResultUtil.error(1008, "赛事编号不能为空");
        }  else if (contest.getName() == null) {
            return ResultUtil.error(1006, "赛事名称不能为空");
        } else if (contest.getStartDate() == null) {
            return ResultUtil.error(1015, "赛事开始时间不能为空");
        } else if (contest.getEndDate() == null) {
            return ResultUtil.error(1016, "赛事截止时间不能为空");
        }
        int result = contestService.editContest(contest);
        if (result > 0) {
            return ResultUtil.success("修改赛事信息成功");
        } else {
            return ResultUtil.error(1011, "修改赛事信息失败");
        }
    }
    @DeleteMapping("/removeContest")
    public Result removeContest(@RequestParam Integer contestId) {
        int i = contestService.removeContest(contestId);
        if (i > 0) {
            return ResultUtil.success("删除赛事成功");
        } else {
            return ResultUtil.error(2003, "删除赛事失败");
        }
    }

    @PostMapping("/addContestNotice")
    public Result addContestNotice(@RequestBody ContestNotice contestNotice) {
        if (contestNotice.getContestId() == null) {
            return ResultUtil.error(1009, "赛事编号不能为空");
        } else if (contestNotice.getTitle() == null) {
            return ResultUtil.error(1010, "公告标题不能为空");
        }
        int result  = contestService.addContestNotice(contestNotice);
        if (result > 0) {
            return ResultUtil.success("发布赛事公告成功");
        } else {
            return ResultUtil.error(1012, "发布赛事公告失败");
        }
    }

    @GetMapping("/getContestNoticeList")
    public Result getContestNoticeList(@RequestParam Integer contestId) {
        List<ContestNotice> contestNoticeList = contestService.getContestNoticeList(contestId);
        return ResultUtil.success(contestNoticeList);
    }

    @PostMapping("/editContestNotice")
    public Result editContestNotice(@RequestBody ContestNotice contestNotice) {
        int result = contestService.editContestNotice(contestNotice);
        if (result > 0) {
            return ResultUtil.success("修改赛事公告成功");
        } else {
            return ResultUtil.error(1013, "修改赛事公告失败");
        }
    }

    @DeleteMapping("/deleteContestNotice")
    public Result deleteContestNotice(@RequestParam Integer contestNoticeId) {
        int result = contestService.deleteContestNotice(contestNoticeId);
        if (result > 0) {
            return ResultUtil.success("删除赛事公告成功");
        } else {
            return ResultUtil.error(1014, "删除赛事公告失败");
        }
    }
}
